package com.fr.data.impl;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.data.Dictionary.MV;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JQNode
{
  public static final String ID_SPLIT = "-";
  private String id;
  private String text;
  private String value;
  private boolean isExpand;
  private boolean hasChildren;
  private boolean showCheck;
  private boolean complete;
  private int checkState;

  public JQNode(String paramString1, String paramString2, String paramString3, boolean paramBoolean)
  {
    this(paramString1, paramString2, paramString3, paramBoolean, true);
  }

  public JQNode(String paramString1, String paramString2, String paramString3, boolean paramBoolean1, boolean paramBoolean2)
  {
    this(paramString1, paramString2, paramString3, paramBoolean1, paramBoolean2, false, false, 0);
  }

  public JQNode(String paramString1, String paramString2, String paramString3, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, boolean paramBoolean4, int paramInt)
  {
    this.id = paramString1;
    this.text = paramString3;
    this.value = paramString2;
    this.isExpand = paramBoolean3;
    this.hasChildren = paramBoolean1;
    this.showCheck = paramBoolean2;
    this.complete = paramBoolean4;
    this.checkState = paramInt;
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof JQNode))
      return false;
    JQNode localJQNode = (JQNode)paramObject;
    return (this.id == localJQNode.id);
  }

  public static JSONArray toJSONArray(List paramList)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("[");
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      JQNode localJQNode = (JQNode)localIterator.next();
      localStringBuffer.append("{id:");
      localStringBuffer.append("\"" + localJQNode.id + "\"");
      localStringBuffer.append(",text:");
      localStringBuffer.append("\"" + localJQNode.text + "\"");
      localStringBuffer.append(",value:");
      localStringBuffer.append("\"" + localJQNode.value + "\"");
      localStringBuffer.append(",showcheck:");
      localStringBuffer.append(localJQNode.showCheck);
      localStringBuffer.append(",complete:");
      localStringBuffer.append(localJQNode.complete);
      localStringBuffer.append(",isexpand:");
      localStringBuffer.append(localJQNode.isExpand);
      localStringBuffer.append(",checkstate:");
      localStringBuffer.append(localJQNode.checkState);
      localStringBuffer.append(",hasChildren:");
      localStringBuffer.append(localJQNode.hasChildren);
      localStringBuffer.append("},");
    }
    if (localStringBuffer.length() > 2)
      localStringBuffer.substring(0, localStringBuffer.length() - 3);
    localStringBuffer.append("]");
    try
    {
      return new JSONArray(localStringBuffer.toString().replaceAll(",]", "]"));
    }
    catch (JSONException localJSONException)
    {
    }
    return new JSONArray();
  }

  public static class Node
  {
    private String layer;
    private Dictionary.MV mv;

    public Node(String paramString, Dictionary.MV paramMV)
    {
      this.layer = paramString;
      this.mv = paramMV;
    }

    public boolean equals(Object paramObject)
    {
      return ((paramObject instanceof Node) && (ComparatorUtils.equals(((Node)paramObject).layer, this.layer)) && (ComparatorUtils.equals(((Node)paramObject).mv, this.mv)));
    }
  }

  private static class Recursion
  {
    protected StringBuffer sb = new StringBuffer();

    public void recursionFn(List paramList, JQNode.QNode paramQNode)
      throws Exception
    {
      if (hasChild(paramList, paramQNode))
      {
        this.sb.append("{id:");
        this.sb.append(paramQNode.id);
        this.sb.append(",text:");
        this.sb.append(paramQNode.text);
        this.sb.append(",value:");
        this.sb.append(paramQNode.value);
        this.sb.append(",showcheck:");
        this.sb.append(paramQNode.showCheck);
        this.sb.append(",complete:true");
        this.sb.append(",isexpand:");
        this.sb.append(paramQNode.isExpand);
        this.sb.append(",checkstate:");
        this.sb.append(paramQNode.checkState);
        this.sb.append(",hasChildren:true");
        this.sb.append(",ChildNodes:[");
        List localList = getChildList(paramList, paramQNode);
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext())
        {
          JQNode.QNode localQNode = (JQNode.QNode)localIterator.next();
          recursionFn(paramList, localQNode);
        }
        this.sb.append("]},");
      }
      else
      {
        this.sb.append("{id:");
        this.sb.append(paramQNode.id);
        this.sb.append(",text:");
        this.sb.append(paramQNode.text);
        this.sb.append(",value:");
        this.sb.append(paramQNode.value);
        this.sb.append(",showcheck:");
        this.sb.append(paramQNode.showCheck);
        this.sb.append(",complete:true");
        this.sb.append(",isexpand:");
        this.sb.append(paramQNode.isExpand);
        this.sb.append(",checkstate:");
        this.sb.append(paramQNode.checkState);
        this.sb.append(",hasChildren:false},");
      }
    }

    public boolean hasChild(List paramList, JQNode.QNode paramQNode)
    {
      return (getChildList(paramList, paramQNode).size() > 0);
    }

    public List getChildList(List paramList, JQNode.QNode paramQNode)
    {
      ArrayList localArrayList = new ArrayList();
      Iterator localIterator = paramList.iterator();
      while (true)
      {
        JQNode.QNode localQNode;
        do
        {
          if (!(localIterator.hasNext()))
            break label65;
          localQNode = (JQNode.QNode)localIterator.next();
        }
        while (!(ComparatorUtils.equals(localQNode.parentId, paramQNode.id)));
        localArrayList.add(localQNode);
      }
      label65: return localArrayList;
    }

    public String modify(String paramString)
    {
      return "[" + paramString + "]".replaceAll(",]", "]");
    }
  }

  public static class QNode
  {
    protected String id;
    protected String parentId;
    protected String text;
    protected String value;
    protected boolean showCheck;
    protected int checkState;
    protected boolean isExpand;

    public QNode(String paramString1, String paramString2)
    {
      this(paramString1, paramString2, "root", "root");
    }

    public QNode(String paramString1, String paramString2, String paramString3, String paramString4)
    {
      this(paramString1, paramString2, paramString3, paramString4, false, 1, true);
    }

    public QNode(String paramString1, String paramString2, String paramString3, String paramString4, boolean paramBoolean1, int paramInt, boolean paramBoolean2)
    {
      this.id = paramString1;
      this.parentId = paramString2;
      this.text = paramString4;
      this.value = paramString3;
      this.showCheck = paramBoolean1;
      this.checkState = paramInt;
      this.isExpand = paramBoolean2;
    }

    public static JSONArray createTreeNode(List paramList)
      throws Exception
    {
      paramList.add(new QNode("0", "-1"));
      JQNode.Recursion localRecursion = new JQNode.Recursion();
      localRecursion.recursionFn(paramList, new QNode("0", "-1"));
      String str = localRecursion.modify(localRecursion.sb.toString());
      str = str.substring(str.indexOf("ChildNodes") + "ChildNodes".length() + 1, str.length() - 2);
      return new JSONArray(str);
    }
  }
}